package com.junzhiit.nengji.cms.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.junzhiit.nengji.cms.service.RevenueSettingService;
import com.junzhiit.nengji.cms.web.exception.QuestionNotFoundException;
import com.junzhiit.nengji.cms.web.exception.RevenueSettingIsExistException;
import com.junzhiit.nengji.cms.web.util.ViewObjectUtil;
import com.junzhiit.nengji.cms.web.vo.DataTablesParamVO;
import com.junzhiit.nengji.cms.web.vo.ResultVO;
import com.junzhiit.nengji.cms.web.vo.RevenueSettingVO;
import com.junzhiit.nengji.dao.RevenueSettingDAO;
import com.junzhiit.nengji.model.RevenueSetting;

@Service
public class RevenueSettingServiceImpl extends BaseServiceImpl implements RevenueSettingService {

	@Autowired
	private RevenueSettingDAO revenueSettingDAO;
	
	@Override
	public Map<String, Object> findAll(DataTablesParamVO dtp,Integer type) {
		List<RevenueSetting> list = revenueSettingDAO.findAll(dtp.getStart(), dtp.getLength(),type);

		List<RevenueSettingVO> listVO = new ArrayList<RevenueSettingVO>();
		for (RevenueSetting revenueSetting : list) {
			RevenueSettingVO revenueSettingVO = new RevenueSettingVO();
			BeanUtils.copyProperties(revenueSetting, revenueSettingVO);
			listVO.add(revenueSettingVO);
		}
		
		int totalCount = revenueSettingDAO.getTotalCount(type);
		return ViewObjectUtil.dataTables(listVO, dtp.getDraw(), totalCount);
	}
	
	@Override
	public ResultVO add(RevenueSettingVO revenueSettingVO) {
		ResultVO result = new ResultVO();
		int count = revenueSettingDAO.checkType(revenueSettingVO.getType());
		if(count > 0 ){
			throw new RevenueSettingIsExistException("类型已存在");
		}
		RevenueSetting revenueSetting = new RevenueSetting();
		BeanUtils.copyProperties(revenueSettingVO, revenueSetting);
		revenueSettingDAO.insert(revenueSetting);
		return result;
	}
	
	@Override
	public ResultVO findById(String id) {
		ResultVO resultVO = new ResultVO();
		RevenueSetting revenueSetting = revenueSettingDAO.query(id);
		if (revenueSetting == null) {
			throw new QuestionNotFoundException("该流水信息不存在！");
		}
		RevenueSettingVO revenueSettingVO = new RevenueSettingVO();
		BeanUtils.copyProperties(revenueSetting, revenueSettingVO);
		resultVO.setData(revenueSettingVO);
		return resultVO;
	}
	
	@Override
	public ResultVO edit(RevenueSettingVO revenueSettingVO) {
		ResultVO resultVO = new ResultVO();
		RevenueSetting revenueSetting = revenueSettingDAO.query(revenueSettingVO.getId());
		
		if (revenueSetting == null) {
			throw new QuestionNotFoundException("该提问不存在！");
		}
		
		BeanUtils.copyProperties(revenueSettingVO, revenueSetting);
		revenueSetting.setUpdatedTime(new Date());
		resultVO.setData(revenueSettingDAO.update(revenueSetting));
		return resultVO;
	}
	
	@Override
	public ResultVO delele(String id) {
		ResultVO resultVO = new ResultVO();
		try {
			resultVO.setData(revenueSettingDAO.updateToDelete(id));
		} catch (Exception e) {
			throw new QuestionNotFoundException("该提问不存在！");
		}
		return resultVO;
	}
}
